const users = []

// 当用户加入聊天，把用户信息保存到数组中，返回当前用户
const userJoin = (id, username, room) => {
    const user = { id, username, room }
    users.push(user)
    return user
}

// 根据id来获取用户信息
const getCurrentUser = (id) => {
    return users.find(user => user.id === id)
}

// 离开房间 
const userLeave = (id) => {
    const index = users.findIndex((user) => user.id == id)
    if (index != -1) {
        return users.splice(index, 1)[0]
    }
}

// 获取每个房间的用户 
const getRoomUsers = (room) => {
    return users.filter((user) => user.room == room)
}

module.exports = {
    userJoin,
    getCurrentUser,
    userLeave,
    getRoomUsers
}